package util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;
import java.net.UnknownHostException;

import static java.lang.Integer.parseInt;

public class CheckUtil {

    private static final Logger logger = LoggerFactory.getLogger(CheckUtil.class);
    public  boolean ipCheck(String text) {
        if (text != null && !text.isEmpty()) {
            String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\.(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\.(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\.(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
            if (text.matches(regex)) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    public boolean testCaseIdCheck(String text){
        if(!("h".equals(text)||"q".equals(text))){
            int id;
            try {
                id = parseInt(text);
            } catch (Exception e){
                return false;
            }
            if (!(id>=0&&id<=6)){
                return false;
            }
        }
        return true;
    }

    public InetAddress hostCheck(String host){
        InetAddress address;
        try {
            address = InetAddress.getByName(host);
        } catch (UnknownHostException e) {
            logger.error("Unknown host: " + host);
            return null;
        }
        return address;
    }

    public boolean DirectoryCheck(String fileName){
        return fileName.contains("/");
    }
}
